package com.myplans.server.service;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;

import com.myplans.criteria.CategorySearchCriteria;
import com.myplans.criteria.PlanSearchCriteria;
import com.myplans.server.util.ServiceLoader;
import com.myplans.shared.AndroidPlan;
import com.myplans.shared.Category;
import com.myplans.vo.CategoryValue;
import com.myplans.vo.PlanValue;

public class AndroidPlanServiceImpl implements PlanService {

	@Override
	public AndroidPlan addPlan(AndroidPlan plan) {
		PlanValue planValue = new PlanValue();
		try {
			BeanUtils.copyProperties(planValue, plan);
			planValue = ServiceLoader.getServiceInstance().getPlanService().createPlan(planValue);
			BeanUtils.copyProperties(plan, planValue);
			
			return plan;
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public AndroidPlan[] getPlans(String categoryName, String searchString) {
		PlanSearchCriteria searchCriteria = new PlanSearchCriteria();
		if (categoryName != null) {
			searchCriteria.setCategory(categoryName);
		}
		if (searchString != null) {
			searchCriteria.setCategory(searchString);
			searchCriteria.setLocation(searchString);
			searchCriteria.setDescription(searchString);
			searchCriteria.setName(searchString);
		}
		List<PlanValue> planValues = ServiceLoader.getServiceInstance().
				getPlanService().getPlans(searchCriteria);
		AndroidPlan[] plans = new AndroidPlan[planValues.size()];
		
		try {
			for (int i = 0; i < planValues.size(); i++) {
				AndroidPlan plan = new AndroidPlan();
				PlanValue planValue = planValues.get(i);
				BeanUtils.copyProperties(plan, planValue);
				plans[i] = plan;
			}
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return plans;
	}
	
	@Override
	public Category[] getCategories() {
		CategorySearchCriteria searchCriteria = new CategorySearchCriteria();
		List<CategoryValue> planValues = ServiceLoader.getServiceInstance().
				getPlanService().getCategories(searchCriteria);
		Category[] categories = new Category[planValues.size()];
		
		try {
			for (int i = 0; i < planValues.size(); i++) {
				Category category = new Category();
				CategoryValue categoryValue = planValues.get(i);
				BeanUtils.copyProperties(category, categoryValue);
				categories[i] = category;
			}
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return categories;
	}

}
